package com.haoyun.mirage.user.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Sets;
import com.haoyun.common.constant.CommonConstant;
import com.haoyun.common.core.BaseException;
import com.haoyun.mirage.user.common.utils.TreeUtil;
import com.haoyun.mirage.user.mapper.SysMenuMapper;
import com.haoyun.mirage.user.model.dto.MenuDTO;
import com.haoyun.mirage.user.model.entity.SysMenu;
import com.haoyun.mirage.user.model.vo.MenuTree;
import com.haoyun.mirage.user.service.ISysMenuService;
import com.xiaoleilu.hutool.lang.Assert;
import com.xiaoleilu.hutool.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 菜单权限表 服务实现类
 * </p>
 *
 * @author twg
 * @since 2018-05-30
 */
@Slf4j
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

    @Autowired
    private SysMenuMapper menuMapper;

    @Override
    public List<MenuDTO> findMenuByRoleCode(String roleCode) {
        Assert.notBlank(roleCode, "角色编号不能为空！");
        return menuMapper.findMenuByRoleCode(roleCode);
    }

    @Override
    public List<MenuDTO> findMenuByRoleId(Long roleId) {
        Assert.notNull(roleId,"角色ID不能为空");
        return menuMapper.findMenuByRoleId(roleId);
    }

    @Override
    public String[] findPermission(String... roles) {
        Assert.notEmpty(roles, "角色编号不能为空！");
        List<MenuDTO> menus = menuMapper.findMenuByRoleCodes(roles);
        Set<String> permissions = Sets.newHashSet();
        for (MenuDTO menu : menus) {
            if (StrUtil.isNotEmpty(menu.getPermission())) {
                String permission = menu.getPermission();
                permissions.add(permission);
            }
        }
        return permissions.toArray(new String[permissions.size()]);
    }

    @Override
    @Transactional
    public Boolean deleteMenu(Long id, String... roleCodes) {
        Assert.isNull(id, "菜单ID不能为空！");
        Assert.notEmpty(roleCodes, "角色编号不能为空！");
        // 删除当前节点
        this.deleteById(id);
        //删除角色菜单关系
        menuMapper.deleteRoleMenuByMenuIdAndRoleCodes(id, roleCodes);
        // 删除父节点为当前节点的节点
        return this.delete(new EntityWrapper<SysMenu>().eq("parent_id", id));
    }

    @Override
    public Boolean updateMenuById(SysMenu sysMenu) {
        return this.updateById(sysMenu);
    }

    @Override
    public List<MenuTree> findMenuTreeByRoleCodes(String... roleCodes) {
        Assert.notEmpty(roleCodes, "角色编号不能为空！");
        List<MenuDTO> menus = menuMapper.findMenuByRoleCodes(roleCodes);
        final List<MenuTree> menuTreeList = new ArrayList<>();
        menus.forEach(m -> {
            if (CommonConstant.MENU.equals(m.getType())) {
                menuTreeList.add(new MenuTree(m));
            }
        });
        return TreeUtil.build(menuTreeList, 0L);
    }

    @Override
    public Boolean insertMenu(SysMenu menu) throws BaseException {
        if (this.isExist(menu.getMenuName())){
            throw new BaseException("菜单 ["+menu.getMenuName()+"] 已存在！");
        }
        return this.insert(menu);
    }

    @Override
    public Boolean isExist(String menuName) {
        Assert.notBlank(menuName, "菜单名不能为空！");
        SysMenu menu = this.selectOne(new EntityWrapper<SysMenu>().eq("menu_name", menuName));
        if (menu == null) {
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }
}
